Ext.define('NF.view.farm.CreateLivestock', {
    extend: 'Ext.window.Window',
    alias:'widget.createLivestock',
    autoHeight:true,
    femaleLivestockStore:null,
    maleLivestockStore:null,
    selectedFarmID:-1,
    width: 400,
    layout: {
        type: 'fit'
    },
    initComponent: function() {
        var self = this;
        self.items = [
            {
                xtype: 'form',
                frame: true,
                autoHeight: true,
                autoWidth: true,
                autoScroll:true,
                url:'livestock/createLivestock.json',
                bodyPadding: 10,
                items: [
                    {
                        xtype: 'combobox',
                        name: 'farmId',
                        fieldLabel: 'Farm',
                        store: 'Farms',
                        anchor: '100%',
                        displayField:'name',
                        valueField:'id',
                        allowBlank:false,
                        listeners:{
                            select:function(comboBox, selectedRecords) {
                                var self = this;
                                self.ownerCt.ownerCt.selectedFarmID = selectedRecords[0].data.id;
                                self.ownerCt.ownerCt.createFemaleLivestockStoreOfFarm();
                                self.ownerCt.ownerCt.createMaleLivestockStoreOfFarm();
                            }
                        }
                    },
                    {
                        xtype: 'combobox',
                        name: 'motherId',
                        fieldLabel: 'Mother',
                        store: self.createFemaleLivestockStoreOfFarm(),
                        anchor: '100%',
                        displayField:'displayField',
                        queryMode: 'local',
                        valueField:'id',
                        allowBlank:false
                    },
                    {
                        xtype: 'combobox',
                        name: 'fatherId',
                        fieldLabel: 'Father',
                        store: self.createMaleLivestockStoreOfFarm(),
                        anchor: '100%',
                        displayField:'displayField',
                        queryMode: 'local',
                        valueField:'id',
                        allowBlank:false
                    },
                    {
                        xtype: 'textfield',
                        name: 'givenId',
                        allowBlank:false,
                        fieldLabel: 'Given ID',
                        anchor: '100%'
                    },
                    {
                        xtype: 'textfield',
                        name: 'name',
                        allowBlank:false,
                        fieldLabel: 'Name',
                        anchor: '100%'
                    },
                    {
                        xtype: 'textfield',
                        name: 'genetics',
                        allowBlank:false,
                        fieldLabel: 'Genetics',
                        anchor: '100%'
                    },
                    {
                        xtype: 'numberfield',
                        name: 'height',
                        allowBlank:false,
                        fieldLabel: 'Height',
                        anchor: '100%'
                    },
                    {
                        xtype: 'numberfield',
                        name: 'listPrice',
                        allowBlank:false,
                        fieldLabel: 'List Price',
                        anchor: '100%'
                    },
                    {
                        xtype: 'numberfield',
                        fieldLabel: 'Purchase Price',
                        allowBlank:false,
                        name: 'purchasePrice',
                        anchor: '100%'
                    },
                    {
                        xtype: 'numberfield',
                        name: 'sellingPrice',
                        fieldLabel: 'Selling Price',
                        allowBlank:false,
                        anchor: '100%'
                    },
                    {
                        xtype: 'datefield',
                        name: 'purchaseDate',
                        allowBlank:false,
                        fieldLabel: 'Purchase Date',
                        anchor: '100%'
                    },
                    {
                        xtype: 'datefield',
                        name: 'dateOfBirth',
                        allowBlank:false,
                        fieldLabel: 'Birth Date',
                        anchor: '100%'
                    },
                    {
                        xtype: 'numberfield',
                        name: 'weight',
                        allowBlank:false,
                        fieldLabel: 'Weight',
                        anchor: '100%'
                    },
                    {
                        xtype: 'combobox',
                        triggerAction: 'all',
                        name: 'gender',
                        fieldLabel: 'Gender',
                        store: 'Gender',
                        anchor: '100%',
                        displayField:'description',
                        valueField:'type',
                        allowBlank:false
                    },
                    {
                        xtype: 'combobox',
                        triggerAction: 'all',
                        name: 'statusType',
                        fieldLabel: 'Status Type',
                        store: 'LivestockStatusType',
                        anchor: '100%',
                        displayField:'description',
                        valueField:'id',
                        allowBlank:false
                    }
                ],
                buttons:[
                    {
                        text: 'Submit',
                        handler: function() {
                            var self=this;
                            var form = this.up('form').getForm(); // get the basic form
                            if (form.isValid()) { // make sure the form contains valid data before submitting
                                 form.submit({
                                    success: function(form, action) {
                                        var farmTab = Ext.getCmp('farmTabsId');
                                        farmTab.addOrActivateTab({
                                            xtype:'livestockMasterDetails',
                                            itemId:Ext.id('livestockMasterDetails'),
                                            context:{id:action.result.livestock[0].id,name:action.result.livestock[0].name}
                                        });
                                       self.up('window').close();
                                    },
                                    failure: function(form, action) {
                                        Ext.Msg.alert('Failed', action.result.msg);
                                    }
                                 });
                            } else { // display error alert if the data is invalid
                                Ext.Msg.alert('Invalid Data', 'Please correct form errors.')
                            }
                        }
                    },
                    {
                        text: 'Reset',
                        handler:function(){
                            var self=this;
                            this.up('form').getForm().reset();
                        }
                    }
                ]
            }
        ];
        this.callParent(arguments);
        this.setTitle("Add Livestock Details")
    },
    createMaleLivestockStoreOfFarm:function(farmId){
        var self=this;
        if(self.maleLivestockStore===null || self.maleLivestockStore === undefined){
            self.maleLivestockStore = Ext.create('Ext.data.Store',{
                   model:'NF.model.FarmLivestockList',
                   autoLoad:false,
                   proxy:{
                       type:'ajax',
                       url:'farms/getMaleLivestockList.json',
                       extraParams:{'id':self.selectedFarmID},
                       reader:{
                          type:'json',
                          root:'farmLivestockList'
                       }
                   }
                });
        }else{
            self.maleLivestockStore.getProxy().extraParams.id = self.selectedFarmID;
        }
        self.maleLivestockStore.load();
        return self.maleLivestockStore;
    },
    createFemaleLivestockStoreOfFarm:function() {
        var self=this;
        if (self.femaleLivestockStore === null || self.femaleLivestockStore === undefined) {
            self.femaleLivestockStore = Ext.create('Ext.data.Store', {
                model:'NF.model.FarmLivestockList',
                autoLoad:false,
                proxy:{
                    type:'ajax',
                    url:'farms/getFemaleLivestockList.json',
                    extraParams:{'id':self.selectedFarmID},
                    reader:{
                        type:'json',
                        root:'farmLivestockList'
                    }
                }
            });
        } else {
            self.femaleLivestockStore.getProxy().extraParams.id = self.selectedFarmID;
        }
        self.femaleLivestockStore.load();
        return  self.femaleLivestockStore;
    }
});